/*************************************************************************
*																		 *
*				code by Miles Chen (v-mich) 2007.9.27					 *
*																		 *
*																		 *
*************************************************************************/

#pragma once

#include "commdef-int.h"
#include "System.Windows.Forms.Form.h"

namespace System{namespace Windows{namespace Forms{

	int AppMain(APPENTRY lpEntry)
	{
		HRESULT hr = CoInitialize(NULL);
		if (SUCCEEDED(hr))
		{	
			InitCommonControls();

			lpEntry();

			CoUninitialize();
		}
		else
		{
			MessageBoxW(NULL, L"CoInitialize error!", L"Error", MB_OK | MB_ICONERROR);
		}
		return 0;
	}

	BOOL SingleInstance(LPCWSTR lpszInstanceName)
	{
		HANDLE hMutex = CreateMutexW(NULL, TRUE, lpszInstanceName);
		if(GetLastError() == ERROR_ALREADY_EXISTS)
		{
			ReleaseMutex(hMutex);
			return FALSE;
		}
		return TRUE;
	}


	CForm::CForm()
	{
		IDD = 0;
		m_nAppIconID = 0;
		m_hAppIcon = NULL;
	}

	//CDialogAppBase::~CDialogAppBase(void)
	//{
	//	DeleteObject(m_hBrushDlg);
	//}

	//int CDialogAppBase::ShowDialog(HWND hwndParent)
	//{
	//	DialogBoxParamW(m_hInst, MAKEINTRESOURCEW(m_iFormID), hwndParent, DialogProc, (LPARAM)this);
	//	return 0;
	//}

	//INT_PTR CALLBACK CDialogAppBase::DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
	//{
	//	CDialogAppBase *that = (CDialogAppBase*)GetWindowLongW(hDlg, GWLP_USERDATA);
	//	switch(message)
	//	{
	//	case WM_INITDIALOG:
	//		{
	//			that = (CDialogAppBase*)lParam;
	//			SetWindowLongW(hDlg, GWLP_USERDATA, (LONG)that);
	//			that->OnInitDialog(hDlg);			
	//		}
	//		break;
	//	case WM_CTLCOLORDLG:
	//		{
	//			return that->OnCtlColorDlg((HDC)wParam);
	//		}
	//	case WM_CTLCOLORSTATIC:
	//		{
	//			return that->OnCtlColorStatic((HDC)wParam);
	//		}
	//	case WM_LBUTTONDOWN:
	//		{
	//			that->OnLeftMouseButtonDown(hDlg, lParam);
	//		}
	//		break;
	//	case WM_LBUTTONUP:
	//		{
	//			that->OnLeftMouseButtonUp(hDlg, lParam);
	//		}
	//		break;
	//	case WM_MOUSEMOVE:
	//		{
	//			that->OnMouseMove(hDlg, lParam);
	//		}
	//		break;
	//	case WM_TIMER:
	//		{
	//			that->OnTimer(hDlg, wParam);
	//		}
	//		break;
	//	case WM_COMMAND:
	//		{
	//			switch(LOWORD(wParam))
	//			{
	//			case IDOK:
	//				{
	//					that->OnCommandOK(hDlg);
	//				}
	//				break;
	//			case IDCANCEL:
	//				{
	//					that->OnCommandCancel(hDlg);
	//				}
	//				break;
	//			default:
	//				{
	//					that->OnCommand(hDlg, LOWORD(wParam), HIWORD(wParam));
	//				}
	//				break;
	//			}
	//		}
	//		break;
	//	case WM_CLOSE:
	//		{
	//			that->OnClose(hDlg);
	//		}
	//		break;
	//	case WM_DESTROY:
	//		{
	//			SetWindowLongW(hDlg, GWLP_USERDATA, NULL);
	//		}
	//		break;
	//	case WM_HOTKEY:
	//		{
	//			that->OnHotkey(hDlg, (int)wParam);
	//		}
	//		break;
	//	}
	//	return 0;
	//}

	//void CDialogAppBase::OnHotkey(HWND hDlg, int idHotKey)
	//{
	//}

	//void CDialogAppBase::OnInitDialog(HWND hDlg)
	//{
	//}

	//void CDialogAppBase::OnClose(HWND hDlg)
	//{
	//	EndDialog(hDlg, 0);
	//}
	//void CDialogAppBase::OnCommand(HWND hDlg, WORD wmID, WORD wmEvent)
	//{
	//}
	//void CDialogAppBase::OnTimer(HWND hDlg, DWORD dwTimerID)
	//{
	//}
	//void CDialogAppBase::OnCommandOK(HWND hDlg)
	//{
	//	SendMessageW(hDlg, WM_CLOSE, 0, 0);
	//}

	//void CDialogAppBase::OnCommandCancel(HWND hDlg)
	//{
	//	SendMessageW(hDlg, WM_CLOSE, 0, 0);
	//}

	//void CDialogAppBase::OnLeftMouseButtonDown(HWND hDlg, LPARAM lParam)
	//{
	//	PostMessage(hDlg, WM_NCLBUTTONDOWN, HTCAPTION, lParam);
	//}

	//void CDialogAppBase::OnLeftMouseButtonUp(HWND hDlg, LPARAM lParam)
	//{
	//}

	//void CDialogAppBase::OnMouseMove(HWND hDlg, LPARAM lParam)
	//{
	//}

	//INT_PTR CDialogAppBase::OnCtlColorDlg(HDC hdc)
	//{
	//	return (INT_PTR)m_hBrushDlg;
	//}

	//INT_PTR CDialogAppBase::OnCtlColorStatic(HDC hdc)
	//{
	//	SetTextColor(hdc, RGB(0, 128, 192));
	//	SetBkMode(hdc, TRANSPARENT);
	//	return (INT_PTR)m_hBrushDlg;
	//}

	//BOOL CDialogAppBase::SetWindowTopMost(HWND hDlg, BOOL fTopmost)
	//{
	//	return SetWindowPos(hDlg, fTopmost?HWND_TOPMOST:HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
	//}


}}}


LRESULT System::Windows::Forms::CForm::OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// TODO: Add your message handler code here and/or call default

	DeleteObject(m_hBrushDlg);

	return CDialogImpl::EndDialog(0);
}

LRESULT System::Windows::Forms::CForm::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// TODO: Add your message handler code here and/or call default

	m_hInst = ::GetModuleHandleW(NULL);
	m_hBrushDlg = ::CreateSolidBrush(0xEEEEEE);
	if(m_nAppIconID)
		m_hAppIcon = ::LoadIconW(m_hInst, MAKEINTRESOURCEW(m_nAppIconID));
	if(m_hAppIcon)
		this->SetIcon(m_hAppIcon);

	return TRUE;
}

LRESULT System::Windows::Forms::CForm::OnLButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
	// TODO: Add your message handler code here and/or call default

	this->PostMessageW(WM_NCLBUTTONDOWN, HTCAPTION, lParam);

	return 0;
}

LRESULT System::Windows::Forms::CForm::OnCtlColorStatic(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// TODO: Add your message handler code here and/or call default

	SetTextColor((HDC)wParam, RGB(0, 128, 192));
	SetBkMode((HDC)wParam, TRANSPARENT);
	return (LRESULT)m_hBrushDlg;
}

LRESULT System::Windows::Forms::CForm::OnCtlColorDlg(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// TODO: Add your message handler code here and/or call default

	return (LRESULT)m_hBrushDlg;
}
